<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    list:[] as Array<any>,
    title:''
  })
  interface formQuert {
    type:number
    reason_img:string
    operate_info:string
    guid:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const { type=0 } = form
      await numberApi.handleForbidDevice(form).then(()=>{
        loading.close()
        closeDialog()
        $emit('updateList')
        ToastSuccess(`设备${['','封禁','解封'][type]}成功`)
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      type=1,
      guid=''
    } = query
    Object.assign(form,{
      type,
      guid
    })
    action.title = `${['','封禁','解封'][type]}用户设备`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="45%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="凭证" prop="type">
          <xmUpload listType="picture-card" :data="{type:26}" v-model:file-url="form.reason_img"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="操作说明" >
          <el-input
          :disabled="action.disabled"
          v-model="form.operate_info"
          :autosize="{ minRows: 6, maxRows: 6}"
          type="textarea"
          placeholder="请输入操作说明"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>